const { DataTypes, sequelize } = require("../config/sequelizeCommon");
const { v4: uuidv4 } = require('uuid');

const checkTableExistsQuery = `
SELECT table_name
  FROM information_schema.tables
  WHERE table_schema = 'OrderingSystem' AND table_name = 'tables'
`;

(() => {
    try {
        const existingTables = sequelize.query(checkTableExistsQuery, {
            type: sequelize.QueryTypes.SELECT,
        });

        const Table = sequelize.define("Tables", {
            id: {
                type: DataTypes.UUID,
                primaryKey: true,
                defaultValue: DataTypes.UUIDV4, // 使用 UUID v4 生成默认值
            },
            // 桌名
            table_name: {
                type: DataTypes.STRING(50),
                allowNull: false,
            },
            // 桌号
            table_number: {
                type: DataTypes.STRING(50),
                allowNull: false,
            },
            
        });

        if (existingTables.length === 0) {
            // 表不存在，创建表
            Table.sync();
        }

        // 导出 Customer
        module.exports = { Table };
    } catch (error) {
        console.error("Error checking or creating user table:", error);
    }
})();
